package com.hollycrm.ahunicom.sheet.maintenance.hibernate;

import java.util.Collections;
import java.util.List;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.hollycrm.ahunicom.sheet.business.hibernate.DepartPermission;
import com.hollycrm.ahunicom.sheet.maintenance.DepartPermissionManager;

public class DepartPermissionManagerImpl extends HibernateDaoSupport implements
		DepartPermissionManager {

	public void saveOrUpdateDepartPermission(DepartPermission departPermission) {
		try {
			DepartPermission temp = findDepartPermission(departPermission
					.getDepartID());
			if (temp == null)
				getHibernateTemplate().saveOrUpdate(departPermission);
			else {
				temp.setIsMain(departPermission.getIsMain());
				temp.setSheet_status(departPermission.getSheet_status());
				temp.setSheetCategoryID(departPermission.getSheetCategoryID());
				getHibernateTemplate().update(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void deleteDepartPermission(DepartPermission departPermission) {
		try {
			DepartPermission temp = findDepartPermission(departPermission
					.getDepartID());
			if (temp == null)
				return;
			else
				getHibernateTemplate().delete(temp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public DepartPermission findDepartPermission(String departID) {
		try {
			String hql = "from s in " + DepartPermission.class
					+ " where s.departID='" + departID + "'";
			List list = getHibernateTemplate().find(hql);
			if (list.size() > 0)
				return (DepartPermission) list.get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List findAll() {
		try {
			String hql = "from s in " + DepartPermission.class;
			List list = getHibernateTemplate().find(hql);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Collections.emptyList();
	}

}
